package com.pic4493.adapter;

import java.util.ArrayList;
import java.util.Map;

import android.R.integer;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdpCstLayListView extends BaseAdapter
{
   private static final int NO_ID = -1;
   private LayoutInflater mInflater;
   private int resource;
   private ArrayList<? extends Map<String, ?>> data;
   private String[] from;
   private int[] to;
   private View.OnClickListener onClickListener;
   private int buttonId = NO_ID;

   public AdpCstLayListView(Context context, ArrayList<? extends Map<String, ?>> data, int resouce, String[] from, int[] to)
   {
	  this.data = data;
	  this.resource = resouce;
	  this.data = data;
	  this.from = from;
	  this.to = to;
	  this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   }

   public AdpCstLayListView(Context context, ArrayList<? extends Map<String, ?>> data, int resouce, String[] from, int[] to, int buttonId,View.OnClickListener onClickListener)
   {
	  this.data = data;
	  this.resource = resouce;
	  this.data = data;
	  this.from = from;
	  this.to = to;
	  this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	  this.onClickListener = onClickListener;
	  this.buttonId = buttonId;
   }

   @Override
   public int getCount()
   {
	  return data.size();
   }

   @Override
   public Object getItem(int position)
   {
	  return data.get(position);
   }

   @SuppressWarnings("unchecked")
   public String get(int position, Object key)
   {
	  Map<String, ?> map = (Map<String, ?>) getItem(position);
	  return map.get(key).toString();
   }

   @Override
   public long getItemId(int position)
   {
	  return position;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent)
   {
	  convertView = mInflater.inflate(resource, null);

	  if(buttonId!=NO_ID)
	  {
		 View btn=(View)convertView.findViewById(buttonId);
		 btn.setTag(position);
		 btn.setClickable(true);
		 btn.setOnClickListener(this.onClickListener);
	  }
	  
	  Map<String, ?> item = data.get(position);
	  int count = to.length;
	  for (int i = 0; i < count; i++)
	  {
		 View v = convertView.findViewById(to[i]);
		 bindView(v, item, from[i],to[i]);
	  }
	  convertView.setTag(position);
	  return convertView;
   }

   /**
    * 绑定视图 @ param view @ param item @ param from
    */
   private void bindView(View view, Map<String, ?> item, String from, int to)
   {
	  Object data = item.get(from);
	  if (view instanceof TextView)
	  {
		 ((TextView) view).setText(data == null ? "" : String.valueOf(data));
	  }
	  if (view instanceof ImageView)
	  {
		 if (data instanceof Drawable)
		 {
			((ImageView) view).setImageDrawable(data == null ? null : (Drawable) data);
		 }
		 else if (data instanceof Integer)
		 {
			((ImageView) view).setBackgroundResource(data == null ? null : Integer.parseInt(data.toString()));
		 }
	  }
   }

}
